package com.example.demo.Controller;
import com.example.demo.Entity.Operation;
import com.example.demo.Entity.Statistics;
import com.example.demo.Entity.User;
import com.example.demo.Server.statisticsServer;
import com.google.gson.Gson;
import com.mysql.cj.log.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
public class StatisticsController {
    @Resource
    statisticsServer server;

    int[] result = new int[12];

    int[] resultT = new int[12];

    int[] resultS = new int[12];
    @Resource
    HashMap<String, Object> params;
    @Resource
    Gson gson;
    /*****
     *
     *
     * 查询所有记账记录
     *
     * ******/
    @ResponseBody
    @RequestMapping(value = "/findAll")
    @CrossOrigin
    public List<Statistics> findAll() {
        System.out.println("查询成功");
        return server.findAll();
    }
    /*****
     *
     *
     * 进行全表的模糊查询
     *
     * ******/
    @ResponseBody
    @RequestMapping(value = "/fuzzyQuery",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @CrossOrigin
    public List<Statistics> fuzzyQuery(@RequestBody String query) {
        //前端传过来的是json数据，需要截取，因为单传一个字符串中文会乱码，暂时没有办法解决
        //将前端传来的json数据截取带分号的数据
        String data = query.substring(9,query.length()-1);
        //将前端传来的json数据截取不带分号的数据
        String data2 = query.substring(10,query.length()-2);
        //条件表达式  将前端传来的json数据截取成需要的数据,判断是否存在 存在返回模糊查询的结果 不存在返回所有数据
        return data.equals("\"\"")? server.findAll():server.fuzzyQuery(data2);
    }
    /*****
     *
     *
     * 通过时间查询编号
     *
     * ******/
    @ResponseBody
    @RequestMapping(value = "/findID",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @CrossOrigin
    public String findID(@RequestBody String date) {
        Operation operation = gson.fromJson(date,Operation.class);
        //返回查询到的ID
        return server.findID(operation.getTime());
    }
    /*****
     *
     *
     * 通过ID查询全部内容
     *
     * ******/
    @ResponseBody
    @RequestMapping(value = "/findByID",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @CrossOrigin
    public Statistics findByID(@RequestBody String _id) {

        //返回ID查询到的全部内容
        return server.findByID(_id.substring(0,_id.length()-1));
    }
    /*****
     *
     *
     * 按照id进行删除
     *
     * ******/
    @ResponseBody
    @RequestMapping(value = "/deleteStatistics",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @CrossOrigin
    public String deleteStatistics(@RequestBody String deleteId) {
        //接收前端的数据，不懂为什么会多个等号 = ,所以通过字符串截取获得特定的数据
        deleteId = deleteId.substring(0,deleteId.length()-1);
        //如果删除成功返回success,失败返回lose
        return server.deleteStatistics(Integer.valueOf(deleteId)) == 1?"success":"lose";
    }
    /*****
     *
     *
     * 添加新的账务
     *
     * ******/
    @ResponseBody
    @RequestMapping(value = "/addStatistics",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @CrossOrigin
    public String addStatistics(@RequestBody String addForm) {
        //将接收到的数据转换为 Statistics对象
        Statistics statistics = gson.fromJson(addForm,Statistics.class);
        server.addStatistics(statistics);
        return "success";
    }
    /*****
     *
     *
     * 修改账务记录
     *
     * ******/
    @ResponseBody
    @RequestMapping(value = "/updateStatistics",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @CrossOrigin
    public String updateStatistics(@RequestBody String queryForm) {
        //将接收到的数据转换为 Statistics对象
        Statistics statistics = gson.fromJson(queryForm,Statistics.class);
        return  server.updateStatistics(statistics) == 1? "success":"lose";
    }
    /*****
     *
     *
     * 统计每个月的开支  折线图
     *
     * ******/
    @ResponseBody
    @CrossOrigin
    @RequestMapping(value = "/drawLineChartZHE",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    public Map<String, Object> drawLineChartZHE(@RequestBody String yearX) {

        String year;
        //因为请求头原因，第一次传值对带个等号,将等号去除.  传入格式是xxx=
        if (yearX.length() == 5) {
            year = yearX.replace("=", "");
        }
        else {
            //截取时间 年份+1
            year = (Integer.valueOf(yearX.substring(1, 5)) + 1) + "";
        }

        //重置数组为0
        for (int i = 0; i < result.length; i++) {
            result[i] = 0;
        }

        for (Statistics s : server.findAll()) {
          //  判断年份是否等于查询的年份
            if (s.getDate().substring(0, 4).equals(year)) {
                //查询月份
                String ss = s.getDate().substring(5, 7);
                int sum = Integer.valueOf(s.getTotalPrice());
                if (ss.equals("01"))
                    result[0] += sum;
                else if (ss.equals("02"))
                    result[1] += sum;
                else if (ss.equals("03"))
                    result[2] += sum;
                else if (ss.equals("04"))
                    result[3] += sum;
                else if (ss.equals("05"))
                    result[4] += sum;
                else if (ss.equals("06"))
                    result[5] += sum;
                else if (ss.equals("07"))
                    result[6] += sum;
                else if (ss.equals("08"))
                    result[7] += sum;
                else if (ss.equals("09"))
                    result[8] += sum;
                else if (ss.equals("10"))
                    result[9] += sum;
                else if (ss.equals("11"))
                    result[10] += sum;
                else if (ss.equals("12"))
                    result[11] += sum;
            }
        }
        params.put("January",result[0]);
        params.put("February",result[1]);
        params.put("MarchL",result[2]);
        params.put("April",result[3]);
        params.put("May",result[4]);
        params.put("June",result[5]);
        params.put("July",result[6]);
        params.put("August",result[7]);
        params.put("September",result[8]);
        params.put("October",result[9]);
        params.put("November",result[10]);
        params.put("December",result[11]);
        return params;
    }


    /*****
     *
     *
     * 统计每个月的开支  条形图
     *
     * ******/
    @ResponseBody
    @CrossOrigin
    @RequestMapping(value = "/drawLineChartTiao",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    public Map<String, Object> drawLineChartTiao(@RequestBody String yearX) {

        String year;
        //因为请求头原因，第一次传值对带个等号,将等号去除.  传入格式是xxx=
        if (yearX.length() == 5) {
            year = yearX.replace("=", "");
            System.out.println("第一处加载时间："+year);
        }
        else {
            //截取时间 年份+1
            year = (Integer.valueOf(yearX.substring(1, 5)) + 1) + "";
        }
        //重置数组为0
        for (int i = 0; i < resultS.length; i++) {
            resultS[i] = 0;
        }

        for (Statistics s : server.findAll()) {
            //  判断年份是否等于查询的年份
            if (s.getDate().substring(0, 4).equals(year)) {
                String ss = s.getDate().substring(5, 7);
                int sum = Integer.valueOf(s.getTotalPrice());
                if (ss.equals("01"))
                    resultS[0] += sum;
                else if (ss.equals("02"))
                    resultS[1] += sum;
                else if (ss.equals("03"))
                    resultS[2] += sum;
                else if (ss.equals("04"))
                    resultS[3] += sum;
                else if (ss.equals("05"))
                    resultS[4] += sum;
                else if (ss.equals("06"))
                    resultS[5] += sum;
                else if (ss.equals("07"))
                    resultS[6] += sum;
                else if (ss.equals("08"))
                    resultS[7] += sum;
                else if (ss.equals("09"))
                    resultS[8] += sum;
                else if (ss.equals("10"))
                    resultS[9] += sum;
                else if (ss.equals("11"))
                    resultS[10] += sum;
                else if (ss.equals("12"))
                    resultS[11] += sum;
            }
        }
        params.put("January",resultS[0]);
        params.put("February",resultS[1]);
        params.put("MarchL",resultS[2]);
        params.put("April",resultS[3]);
        params.put("May",resultS[4]);
        params.put("June",resultS[5]);
        params.put("July",resultS[6]);
        params.put("August",resultS[7]);
        params.put("September",resultS[8]);
        params.put("October",resultS[9]);
        params.put("November",resultS[10]);
        params.put("December",resultS[11]);
        return params;
    }

    /*****
     *
     *
     * 统计每个月的开支  扇形图
     *
     * ******/
    @ResponseBody
    @CrossOrigin
    @RequestMapping(value = "/drawLineChartShan",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    public Map<String, Object> drawLineChartShan(@RequestBody String yearX) {

        String year;
        //因为请求头原因，第一次传值对带个等号,将等号去除.  传入格式是xxx=
        if (yearX.length() == 5) {
            year = yearX.replace("=", "");
        }
        else {
            //截取时间 年份+1
            year = (Integer.valueOf(yearX.substring(1,5)) + 1) + "";
        }
        //重置数组为0
        for (int i = 0; i < resultT.length; i++) {
            resultT[i] = 0;
        }

        System.out.println("扇形时间:"+year);
        for (Statistics s : server.findAll()) {
            //  判断年份是否等于查询的年份
            if (s.getDate().substring(0, 4).equals(year)) {

                String ss = s.getDate().substring(5, 7);
                int sum = Integer.valueOf(s.getTotalPrice());
                if (ss.equals("01"))
                    resultT[0] += sum;
                else if (ss.equals("02"))
                    resultT[1] += sum;
                else if (ss.equals("03"))
                    resultT[2] += sum;
                else if (ss.equals("04"))
                    resultT[3] += sum;
                else if (ss.equals("05"))
                    resultT[4] += sum;
                else if (ss.equals("06"))
                    resultT[5] += sum;
                else if (ss.equals("07"))
                    resultT[6] += sum;
                else if (ss.equals("08"))
                    resultT[7] += sum;
                else if (ss.equals("09"))
                    resultT[8] += sum;
                else if (ss.equals("10"))
                    resultT[9] += sum;
                else if (ss.equals("11"))
                    resultT[10] += sum;
                else if (ss.equals("12"))
                    resultT[11] += sum;
            }
        }
        params.put("January",resultT[0]);
        params.put("February",resultT[1]);
        params.put("MarchL",resultT[2]);
        params.put("April",resultT[3]);
        params.put("May",resultT[4]);
        params.put("June",resultT[5]);
        params.put("July",resultT[6]);
        params.put("August",resultT[7]);
        params.put("September",resultT[8]);
        params.put("October",resultT[9]);
        params.put("November",resultT[10]);
        params.put("December",resultT[11]);

        return params;
    }

}
